package com.xbongbong.saas.service.impl;

import com.xbongbong.pro.domain.entity.ScoreRangeConditionEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * compareValue 值类型可能是 [1],[1,2],[5.01],[2.12,3.12]
 * compareValue.get(0) 可能是 Integer 类型，也可能是 Doule类型。所以这边统一处理为先转为Number类型，在调用 doubleValue()方法。（不能直接用Double强转）
 *
 */
@Component
public class ScoreRangeConditionServiceImpl {

    private static final Logger LOG = LoggerFactory.getLogger(ScoreRangeConditionServiceImpl.class);

    /**
     *
     * @param score 分支
     * @param scoreRangeConditionEntity  区间条件
     * @return 显示的值。如果命中了条件就是showValue；  返回空 表示没有命中该区间。
     */
    public String equal(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    if(score.equals(((Number) compareValue.get(0)).doubleValue())){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    public String noequal(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    if(!score.equals(((Number) compareValue.get(0)).doubleValue())){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    public String greaterequal(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    if(score >= ((Number) compareValue.get(0)).doubleValue()){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    public String greatethan(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    if(score > ((Number) compareValue.get(0)).doubleValue()){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    public String lessequal(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    if(score <= ((Number) compareValue.get(0)).doubleValue()){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    /**
     *
     * @param score
     * @param scoreRangeConditionEntity
     * @return
     */
    public String lessthan(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    //compareValue存的值如果是整数，就是解析出来就是java.lang.Integer，如果是小数，解析出来就是java.lang.Double。
                    // 如果是Integer，强转Double会失败，异常 cannot be cast
                    if(score < ((Number) compareValue.get(0)).doubleValue()){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }

    public String range(Double score, ScoreRangeConditionEntity scoreRangeConditionEntity) {
        try {
            if(Objects.nonNull(scoreRangeConditionEntity)){
                List<Object> compareValue = scoreRangeConditionEntity.getValue();
                if(CollectionUtils.isNotEmpty(compareValue)){
                    Double startValue = ((Number) compareValue.get(0)).doubleValue();
                    Double endValue = ((Number) compareValue.get(1)).doubleValue();
                    //介于两个值是没有先后顺序的。3~5和5~3是一样的
                    if((score >= startValue &&  score <= endValue) || (score >= endValue &&  score <= startValue)){
                        return scoreRangeConditionEntity.getShowValue();
                    }
                }
            }
        }catch (Exception e){
            LOG.error("计算分数区间异常,score-{},scoreRangeConditionEntity-{}-,异常原因-{}-",score.toString(),scoreRangeConditionEntity.toString(),e.getMessage());
            return  null;
        }
        return null;
    }
}
